/usr/bin/env Rscript

bjd <- function(n,e){
  mdiff <- 1.5
  bjdi  <- 99
  nslope <- c(-0.46,-0.22,-2.47,1.23,3.84,-1.16,-0.41,0.28,-0.38,
              0.74,-0.33,1.63,0.43,-0.45,-0.23,-0.2,-2.47,-0.72,
              -0.17,-0.33,0.21,-0.07,-0.61,-2.81,-1.44,0.12,0.48)
  ninter <- c(92.76,65.41,301.73,-96.1,-369.08,141.96,78.95,13.12,67.72,
              -44.57,70.13,-159.74,-26.71,70.78,52.19,52.24,274.28,103.45,
              51.03,69.8,24.14,49.59,101.05,305.2,203.33,21.17,34.3)
  estart <- c(112.6,107.1,105.2,102.7,102.1,97.8,94.7,82.9,72.6,
              106.4,106.4,116.9,106.0,75.8,78.6,76.3,97.7,97.8,
              82.8,92.8,73.7,80.9,84.2,98.3,113.4,109.4,120.5)
  eend   <- c(120.2,112.9,107.1,106.6,104.0,102.2,106.4,94.7,83.0,
              114.4,122.8,122.9,121.7,96.1,95.7,98.4,99.6,102.5,
              104.2,105.8,94.6,92.9,96.1,101.0,116.8,119.4,125.2)
  nstart <- c(40.9,41.4,36.8,30.3,23.2,28.6,40.1,36.3,39.8,
              33.9,35.1,31.2,19.2,36.8,33.8,36.7,32.6,32.7,
              37.1,39.1,39.3,44.2,49.4,28.5,39.9,34.3,23.5)
  nend   <- c(37.4,40.1,41.5,35.1,30.5,23.5,35.3,39.6,35.8,
              39.8,29.7,41.0,26.0,27.7,29.8,32.2,27.9,29.3,
              33.5,34.8,43.6,43.4,42.1,20.9,35.0,35.5,25.8)
  for(i in 1:length(nslope)){
    if(nstart[i]<nend[i]){
      n1 <- nstart[i]
      n2 <- nend[i]
    }else{
      n1 <- nend[i]
      n2 <- nstart[i]
    }
    if(estart[i]<eend[i]){
      e1 <- estart[i]
      e2 <- eend[i]
    }else{
      e1 <- eend[i]
      e2 <- estart[i]
    }
    if(abs(e*nslope[i]+ninter[i]-n) < mdiff & 
         n > n1 - mdiff &
         n < n2 + mdiff &
         e > e1 - mdiff &
         e < e2 + mdiff){
      bjdi <- i
      mdiff <- abs(e*nslope[i]+ninter[i]-n)
    }
  }
  bjdi <- as.integer(bjdi)
  return(bjdi)
}

ml2ms <- function (ml,nlac,elong){
  bjd_id <- as.character(bjd(nlac,elong))
  ms <- switch(bjd_id,
               "99" = 0.07  + 0.97 * ml,    # overall equation
               "1"  = 0.1   + 0.92 * ml,    # Yan Shan He Bei
               "2"  = 0.05  + 0.93 * ml,    # Yin Shan
               "3"  = 0.37  + 0.84 * ml,    # He Lan Shan
               "4"  = -0.03 + 0.99 * ml,    # Min Shan Long Men Shan
               "5"  = 0.01  + 0.98 * ml,    # An Ning He Xiao Jiang
               "6"  = 0.2   + 0.97 * ml,    # Hong He 
               "7"  = 0.11  + 0.93 * ml,    # Hai Yuan Qi Lian
               "8"  = 0.44  + 0.84 * ml,    # A Er Jin
               "9"  = 0.42  + 0.86 * ml,    # Xi Kun Lun
               "10" = -0.01 + 0.98 * ml,    # Fen Wei
               "11" = 0.3   + 0.88 * ml,    # Qin Ling Da Bie Shan
               "12" = 0.68  + 0.77 * ml,    # Yan Lu
               "13" = 0.12  + 0.95 * ml,    # Dong Nan Yan Hai
               "14" = 0.2   + 0.94 * ml,    # Xi Ma La Ya
               "15" = 0.37  + 0.93 * ml,    # Ka La Kun Lun Jia Li
               "16" = 0.12  + 0.96 * ml,    # Ma Ni Yu Shu
               "17" = 0.29  + 0.96 * ml,    # San Jiang
               "18" = 0.17  + 0.98 * ml,    # Xian Shui He
               "19" = -0.01 + 0.99 * ml,    # Dong Kun Lun
               "20" = 0.04  + 0.96 * ml,    # Xi Qin Ling De Ning Ha
               "21" = 0.63  + 0.8  * ml,    # Nan Tian Shan
               "22" = 1.08  + 0.68 * ml,    # Bei Tian Shan
               "23" = 1.02  + 0.7  * ml,    # Fu Yun
               "24" = 0.12  + 0.98 * ml,    # Lang Cang Jiang
               "25" = -0.03 + 0.96 * ml,    # He Bei Ping Yuan
               "26" = 0.44  + 0.83 * ml,    # An Yang He Ze Lin Yi
               "27" = 0.02  + 0.99 * ml     # Tai Wan
        )
  return(ms)
}

ms2ml <- function (ms,nlac,elong){
  bjd_id <- as.character(bjd(nlac,elong))
  ml <- switch(bjd_id,
              "99" = ( ms - 0.07 ) / 0.97,    # overall equation
              "1"  = ( ms - 0.1  ) / 0.92,    # Yan Shan He Bei
              "2"  = ( ms - 0.05 ) / 0.93,    # Yin Shan
              "3"  = ( ms - 0.37 ) / 0.84,    # He Lan Shan
              "4"  = ( ms + 0.03 ) / 0.99,    # Min Shan Long Men Shan
              "5"  = ( ms - 0.01 ) / 0.98,    # An Ning He Xiao Jiang
              "6"  = ( ms - 0.2  ) / 0.97,    # Hong He 
              "7"  = ( ms - 0.11 ) / 0.93,    # Hai Yuan Qi Lian
              "8"  = ( ms - 0.44 ) / 0.84,    # A Er Jin
              "9"  = ( ms - 0.42 ) / 0.86,    # Xi Kun Lun
              "10" = ( ms + 0.01 ) / 0.98,    # Fen Wei
              "11" = ( ms - 0.3  ) / 0.88,    # Qin Ling Da Bie Shan
              "12" = ( ms - 0.68 ) / 0.77,    # Yan Lu
              "13" = ( ms - 0.12 ) / 0.95,    # Dong Nan Yan Hai
              "14" = ( ms - 0.2  ) / 0.94,    # Xi Ma La Ya
              "15" = ( ms - 0.37 ) / 0.93,    # Ka La Kun Lun Jia Li
              "16" = ( ms - 0.12 ) / 0.96,    # Ma Ni Yu Shu
              "17" = ( ms - 0.29 ) / 0.96,    # San Jiang
              "18" = ( ms - 0.17 ) / 0.98,    # Xian Shui He
              "19" = ( ms + 0.01 ) / 0.99,    # Dong Kun Lun
              "20" = ( ms - 0.04 ) / 0.96,    # Xi Qin Ling De Ning Ha
              "21" = ( ms - 0.63 ) / 0.8,     # Nan Tian Shan
              "22" = ( ms - 1.08 ) / 0.68,    # Bei Tian Shan
              "23" = ( ms - 1.02 ) / 0.7,     # Fu Yun
              "24" = ( ms - 0.12 ) / 0.98,    # Lang Cang Jiang
              "25" = ( ms + 0.03 ) / 0.96,    # He Bei Ping Yuan
              "26" = ( ms - 0.44 ) / 0.83,    # An Yang He Ze Lin Yi
              "27" = ( ms - 0.02 ) / 0.99     # Tai Wan
  )
  return(ml)
}


